package com.ruoyi.edh.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.edh.domain.EdhMicroshop;
import com.ruoyi.edh.domain.EdhMicroshopGoods;
import com.ruoyi.edh.domain.EdhShoppingCart;
import com.ruoyi.edh.mapper.EdhMicroshopGoodsMapper;
import com.ruoyi.edh.mapper.EdhMicroshopMapper;
import com.ruoyi.edh.service.IEdhMicroshopGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 易订货-微店上架商品Service业务层处理
 * 
 * @author xxx
 * @date 2020-09-02
 */
@Service
public class EdhMicroshopGoodsServiceImpl implements IEdhMicroshopGoodsService
{
    @Autowired
    private EdhMicroshopGoodsMapper edhMicroshopGoodsMapper;
    @Autowired
    private EdhMicroshopMapper edhMicroshopMapper;

    /**
     * 查询易订货-微店上架商品列表
     * @param title 商品名称
     * @param sortType 排序类别 1奖励最高 2最新发布 3价格升序 4价格降序
     * @return 易订货-微店上架商品
     */
    @Override
    public List<Map<String,Object>> selectEdhMicroshopGoodsList(String title, Integer sortType)
    {
        Long userId = SecurityUtils.getUserId();
        return edhMicroshopGoodsMapper.selectEdhMicroshopGoodsList(title,sortType,userId);
    }

    /**
     * 平台查询团长的商品列表
     *
     * @param title 商品名称
     */
    @Override
    public List<Map<String, Object>> getList(Long userId ,String title,String onSale) {
        return edhMicroshopGoodsMapper.getList(userId,title,onSale);
    }

    /**
     * 查询微店可以上架的商品
     *
     * @param title    商品名称
     * @param sortType 排序类别 1奖励最高 2最新发布 3价格升序 4价格降序
     */
    @Override
    public List<Map<String, Object>> reselllist(String title, Integer sortType) {
        Long userId = SecurityUtils.getUserId();
        return edhMicroshopGoodsMapper.reselllist(title,sortType,userId);
    }

    /**
     * 微店一键上架商品
     * @param goods 商品集合
     */
    @Override
    public Integer onSale(List<Long> goods) {

        Long userId = SecurityUtils.getUserId();

        // 查看用户的微店id
        EdhMicroshop microshop = edhMicroshopMapper.selectEdhMicroshopByUserId(userId);
        if (StringUtils.isNull(microshop)){
            throw new CustomException("您还没有微店,请联系管理员");
        }

        // 查看已删除或者已下架的进行重新上架
        List<Long> notOnSale = edhMicroshopGoodsMapper.getNotOnSale(microshop.getId());
        notOnSale.retainAll(goods);
        if (notOnSale.size() > 0) {
            edhMicroshopGoodsMapper.onSale(notOnSale, microshop.getId());
        }
        // 插入新上架的
        goods.removeAll(notOnSale);
        if (goods.size() == 0 ){
            return 1;
        }
        edhMicroshopGoodsMapper.insertEdhMicroshopGoods(goods,microshop.getId(),userId);
        return 1;
    }

    /**
     * 微店商品全部上架
     */
    @Override
    public Integer allOnSale() {

        // 查询全部可以上架的商品
        List<Long> allReSellGoods = edhMicroshopGoodsMapper.getAllReSellGoods();
        return onSale(allReSellGoods);
    }

    /**
     * 修改易订货-微店上架商品
     * 
     * @param edhMicroshopGoods 易订货-微店上架商品
     * @return 结果
     */
    @Override
    public int updateEdhMicroshopGoods(EdhMicroshopGoods edhMicroshopGoods)
    {
        edhMicroshopGoods.setUpdateTime(DateUtils.getNowDate());
        return edhMicroshopGoodsMapper.updateEdhMicroshopGoods(edhMicroshopGoods);
    }

    /**
     * 批量删除易订货-微店上架商品
     * 
     * @param ids 需要删除的易订货-微店上架商品ID
     * @return 结果
     */
    @Override
    public int deleteEdhMicroshopGoodsByIds(Long[] ids)
    {
        // 购物车商品失效
        for (Long id : ids){
            EdhShoppingCart.killGoodsBymircoGoodsId(id);
        }
        return edhMicroshopGoodsMapper.deleteEdhMicroshopGoodsByIds(ids);
    }

    @Override
    public int deleteEdhMicroshopGoodsByMshopIdAndGodosId(Long goodsId) {
        EdhMicroshop microshop = edhMicroshopMapper.selectEdhMicroshopByUserId(SecurityUtils.getUserId());
        if (StringUtils.isNull(microshop)){
            throw new CustomException("请先开通微店在进行操作");
        }
        return edhMicroshopGoodsMapper.deleteEdhMicroshopGoodsByMshopIdAndGodosId(microshop.getId(),goodsId);
    }
}
